/* CoralCube | Massively Multiplayer Online Role-Playing Game(MMORPG) Emulator.
|* 
|* Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
|*               2008-2010 CoralCube <http://www.CoralCube.com/>
|*
|* This program is free software. You can redistribute it and/or modify it under the terms of 
|* the GNU General Public License as published by the Free Software Foundation, either 
|* version 2 of the License.
|*
|* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, 
|* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
|* See the GNU General Public License for more details.
|*
|* You should have received a copy of the GNU General Public License along with this program. 
|* If not, see <http://www.gnu.org/licenses/>.
|*/

#include "ScriptPCH.h"
#include "BattlegroundAB.h"
#include "BattlegroundWS.h"

class achievement_school_of_hard_knocks : public AchievementCriteriaScript
{
public:
    achievement_school_of_hard_knocks() : AchievementCriteriaScript("achievement_school_of_hard_knocks") { }

    bool OnCheck(Player* source, Unit* /*target*/)
    {
        static uint32 const orphanEntries[6] = {14305, 14444, 22818, 22817, 33533, 33532};
        uint32 currentPet = GUID_ENPART(source->GetCritterGUID());
        for (uint8 i = 0; i < 6; ++i)
        {
            if (currentPet == orphanEntries[i])
                return true;
        }

        return false;
    }
};

class achievement_storm_glory : public AchievementCriteriaScript
{
public:
    achievement_storm_glory() : AchievementCriteriaScript("achievement_storm_glory") { }

    bool OnCheck(Player* source, Unit* /*target*/)
    {
        if (source->GetBattlegroundTypeId() != BATTLEGROUND_EY)
            return false;

        Battleground *pEotS = source->GetBattleground();
        if (!pEotS)
            return false;

        return pEotS->IsAllNodesConrolledByTeam(source->GetTeam());
    }
};

class achievement_resilient_victory : public AchievementCriteriaScript
{
public:
    achievement_resilient_victory() : AchievementCriteriaScript("achievement_resilient_victory") { }

    bool OnCheck(Player* source, Unit* /*target*/)
    {
        Battleground* bg = source->GetBattleground();
        if (!bg)
            return false;

        if (bg->GetTypeID(true) != BATTLEGROUND_AB)
            return false;

        if (!static_cast<BattlegroundAB*>(bg)->IsTeamScores500Disadvantage(source->GetTeam()))
            return false;

        return true;
    }
};

class achievement_bg_control_all_nodes : public AchievementCriteriaScript
{
public:
    achievement_bg_control_all_nodes() : AchievementCriteriaScript("achievement_bg_control_all_nodes") { }

    bool OnCheck(Player* source, Unit* /*target*/)
    {
        Battleground* bg = source->GetBattleground();
        if (!bg)
            return false;

        if (!bg->IsAllNodesConrolledByTeam(source->GetTeam()))
            return false;

        return true;
    }
};

class achievement_save_the_day : public AchievementCriteriaScript
{
public:
    achievement_save_the_day() : AchievementCriteriaScript("achievement_save_the_day") { }

    bool OnCheck(Player* source, Unit* target)
    {
        if (!target)
            return false;

        if (Player const* pTarget = target->ToPlayer())
        {
            if (source->GetBattlegroundTypeId() != BATTLEGROUND_WS || !source->GetBattleground())
                return false;

            BattlegroundWS* pWSG = static_cast<BattlegroundWS*>(source->GetBattleground());
            if (pWSG->GetFlagState(pTarget->GetTeam()) == BG_WS_FLAG_STATE_ON_BASE)
                return true;
        }
        return false;
    }
};

void AddSC_achievement_scripts()
{
    new achievement_school_of_hard_knocks();
    new achievement_storm_glory();
    new achievement_resilient_victory();
    new achievement_bg_control_all_nodes();
    new achievement_save_the_day();
}
